package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *
 * <pre>
 * 学生表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/student")
public class StudentController extends BaseController {
	@Autowired
	private StuClassService stuClassService;
	@Autowired
	private CoursesService coursesService;
	@Autowired
	private UserService userService;
    @Autowired
    private OrganizationNewsService organizationNewsService;

	@GetMapping()
	@RequiresPermissions("jzweb:student:student")
	String Student(String id,Model model){
		model.addAttribute("studentId",id);
		System.out.println("studentId:---Student"+id);

		return "jzweb/student/student";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:student:student")
	public Result<Page<StuClassDO>> list(String id, String phoneNumber, String studentName, String studentUserName) {

        Wrapper<StuClassDO> wrapper = new EntityWrapper<StuClassDO>().orderBy("id", false);
        if (id != null && !id.equals("")){
            wrapper.eq("class_id", Long.valueOf(id));
        }
        List<StuClassDO>  list = new ArrayList<>();
		Page<StuClassDO> page = stuClassService.selectPage(getPage(StuClassDO.class), wrapper);
		for (int i = 0; i < page.getRecords().size(); i++) {
            StuClassDO sclassDO = (StuClassDO) page.getRecords().get(i);
            Wrapper<UserDO> Userwrapper = new EntityWrapper<UserDO>().orderBy("id", false);
            if (phoneNumber != null && !phoneNumber.equals("")) {
                wrapper.eq("mobile", phoneNumber);
            }
            if (studentName != null && !studentName.equals("")) {
                wrapper.eq("name", studentName);
            }
            if (studentUserName != null && !studentUserName.equals("")) {
                wrapper.eq("username", studentUserName);
            }
            wrapper.eq("id", sclassDO.getUserId());
            List<UserDO> userDOS = userService.selectList(Userwrapper);
            if (userDOS.size() > 0) {
                sclassDO.setName(userDOS.get(0).getName());
                sclassDO.setUsername(userDOS.get(0).getUsername());
                sclassDO.setMobile(userDOS.get(0).getMobile());
                sclassDO.setSex(userDOS.get(0).getSex());
                sclassDO.setType(userDOS.get(0).getType());
                OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(userDOS.get(0).getOrgId());
                if(organizationNewsDO != null){
                    sclassDO.setOrgName(organizationNewsDO.getName());
                }else{
                    sclassDO.setOrgName("");
                }
                list.add(sclassDO);
            }
		}

        if(list.size()>0) {
            page.setRecords(list);
        }else {
            list=null;
            page.setRecords(list);
        }

		return Result.ok(page);

	}



    @Log("删除学生表")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:studentClasses:add")
    public Result<String>  remove( Long id){
        stuClassService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除学生表")
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:studentClasses:add")
    public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
        stuClassService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    @GetMapping("/toExamine")
    @RequiresPermissions("jzweb:studentClasses:add")
    String toExamine(Model model, String id){

        model.addAttribute("id",id);

        return "jzweb/student/examine";
    }

    @Log("审核学生")
    @RequestMapping("/examine")
    @ResponseBody
    @RequiresPermissions("jzweb:studentClasses:add")
    public Result<String>  examine(Model model, String id, Integer result, String mark){
	    if(id!=null && !id.equals("")){
            StuClassDO stuClassDO = this.stuClassService.selectById(Long.valueOf(id));
            if(stuClassDO!=null) {
                stuClassDO.setStatus(result);
                stuClassDO.setExamineDate(new Date());
                stuClassDO.setExamineDescribe(mark);
                this.stuClassService.updateById(stuClassDO);
                return Result.ok();
            }else {
                return Result.fail("当前记录不存在！");
            }
        }else {
            return Result.fail("当前记录不存在！");
        }
    }



}
